﻿using Chat_IService;
using Chat_Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using SqlSugar.Extensions;

namespace Chat_Service
{
    public class LarUsersService : BaseService<LarUsers>, ILarUsersService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        public LarUsersService(
            IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 获取当前登录用户信息
        /// </summary>
        public async Task<LarUsers> GetUserInfoAsync()
        {
            return await GetFirstAsync(x => x.Id == GetUserId());
        }
        /// <summary>
        /// 获取当前登录用户ID
        /// </summary>
        public int GetUserId()
        {
            if (_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.UserData) == null)
            {
                return 0;
            }
            var user = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.UserData).Value;
            return JsonConvert.DeserializeObject<LarUsers>(user).Id;
        }
    }
}